import 'package:flutter/material.dart';
import 'package:flutter_application_3/contact_list.dart';
import 'package:flutter_application_3/layout_demo.dart';
import 'package:flutter_application_3/sliver_demo.dart';
import 'package:flutter_application_3/pages/route_animation_demo.dart';
import 'package:flutter_application_3/pages/product_list_page.dart';

import 'package:provider/provider.dart';
import 'providers/cart_provider.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => CartProvider(), 
      child: MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: false, 
      ),
      // home: const MyHomePage(title: '我的第一个应用'),
      home: const CounterPage(),
      ),
    );
  }
}

class CounterPage extends StatefulWidget {
  const CounterPage({super.key});

  @override
  State<CounterPage> createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  void _resetCounter() {
    setState(() {
      _counter = 0;
    });
  }

  void _decrementCounter() {
    setState(() {
      _counter--;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('计数器'),
        actions: [
          IconButton(
            icon: const Icon(Icons.add),
            onPressed: _resetCounter,
            tooltip: '重置',
          ),
        ],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text('你已经按了按钮的次数：'),
            const SizedBox(height: 20),
            Text('$_counter', style: const TextStyle(fontSize: 24,fontWeight: FontWeight.bold,color: Colors.blue)),
            const SizedBox(height: 30),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton.icon(onPressed: _incrementCounter, icon: const Icon(Icons.add), label: const Text('增加'),style: ElevatedButton.styleFrom(padding: const EdgeInsets.all(16))),
                const SizedBox(width: 20),
                ElevatedButton.icon(onPressed: _decrementCounter, icon: const Icon(Icons.remove), label: const Text('减少'),style: ElevatedButton.styleFrom(padding: const EdgeInsets.all(16))),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              
              children: [
                ElevatedButton(onPressed: () {
                  Navigator.push(context, MaterialPageRoute(builder: (context) => const ContactList()));
                }, child: const Text('联系人列表')),
                const SizedBox(width: 20),
                ElevatedButton(onPressed: () {
                  Navigator.push(context, MaterialPageRoute(builder: (context) => const LayoutDemo()));
                }, child: const Text('布局组件')),
                const SizedBox(width: 20),
                ElevatedButton(onPressed: () {
                  Navigator.push(context, MaterialPageRoute(builder: (context) => const SliverDemo()));
                }, child: const Text('Sliver组件')),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(onPressed: () {
                  Navigator.push(context, MaterialPageRoute(builder: (context) => const RouteAnimationDemo()));
                }, child: const Text('路由动画')),
                const SizedBox(width: 20),
                ElevatedButton(onPressed: () {
                  Navigator.push(context, MaterialPageRoute(builder: (context) => ProductListPage()));
                }, child: const Text('购物车')),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: const Center(
        child:  Text('Hello, World!', style: TextStyle(fontSize: 24)),
      ),
    );
  }

  
}
